package com.cspg.design.presenter;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.ArrayAdapter;

import com.cspg.design.CSPGApplication;
import com.cspg.design.MainActivity;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.dto.ProjectDto;
import com.cspg.design.model.ProjectModel;
import com.cspg.design.request.Home;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.widget.CSPGProgressBar;
import com.cspg.design.widget.ToastUtils;


import java.util.ArrayList;
import java.util.List;

import q.rorbin.badgeview.Badge;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class HomeFragmentPresenter implements IPresenter {
    private Home homeModel;
    private Activity activity;

    private CSPGProgressBar cspgProgressBar;

    private Badge badge;
    private List<ProjectDto> projectDtos;

    public HomeFragmentPresenter(Activity activity) {
        this.homeModel = new Home(this);
        this.activity = activity;
    }

    public HomeFragmentPresenter(Activity activity, Badge badge)
    {
        this.homeModel = new Home(this);
        this.activity = activity;
        this.badge = badge;
    }


    public void project()  {
        if(cspgProgressBar == null)
        {
            cspgProgressBar = CSPGProgressBar.show(activity);
        }else {
            cspgProgressBar.show();
        }
        try {

            homeModel.getProject(UserInfo.getInstance().EmployeeID);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void getFailure(String msg) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, msg);
    }

    public void getTokenExpireFailure() {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        Intent intent = new Intent();
        intent.setClass(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    public void requestSuccess(List<ProjectDto> projectDtos) {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (projectDtos != null) {
            processData(projectDtos);
        }
    }

    @Override
    public void netWorkError(String netError) {
//        activity.hideProgress();
//        activity.toast(netError);

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, netError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestFailure(String noDataError) {
//        activity.hideProgress();
////        activity.toast(noDataError);
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, noDataError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestSuccess() {
//        activity.hideProgress();
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (activity instanceof LoginActivity) {
//            addLocalData();
//            LoginActivity loginActivity = (LoginActivity) activity;
//            loginActivity.openNewActivity(MainActivity.class);
        } else {
//            activity.openNewActivity(MainActivity.class);
//            Intent intent = new Intent();
//            intent.setClass(activity, MainActivity.class);
//            activity.startActivity(intent);
        }
    }
    private void processData(final List<ProjectDto> projectDtos)
    {
        this.projectDtos = projectDtos;
        this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (badge!= null)
                {
                    badge.setBadgeNumber(projectDtos.size());
                }
            }
        });
    }

    public List<ProjectDto> getProjectDtos()
    {
        return this.projectDtos;
    }

}
